package dao;

import java.sql.*;

import domain.User;
import util.*;

public class UserDAO {

	private Connection conn;
	private PreparedStatement stmt;
	private ResultSet rs;
	
	private static final String AUTHENTICATE_USER="select * from user where email= ? and password= ? ";
	private static final String SELECT_USER_BY_UID="select * from user where uid= ? ";
	private static final String CREATE_USER="insert into  ";

	public User selectUserByEmailAndPassword(String email,String password)
	{
		conn = JDBCUtil.getConnection();
		
		User user = null;
		try {
			stmt = conn.prepareStatement(AUTHENTICATE_USER);
			stmt.setString(1, email);
			stmt.setString(2, password);
			rs = stmt.executeQuery();
			if (rs.next()) {
				long uid = rs.getLong("uid");
				String firstname = rs.getString("firstName");
				String lastname = rs.getString("lastName");
				String imageURL = rs.getString("imageURL");
				String summary = rs.getString("summary");
				String city = rs.getString("city");
				String tel = rs.getString("tel");
				int available=rs.getInt("isAvailable");
				user=new User(uid,email,password, firstname,lastname,imageURL, summary,city,tel,available==1);
			}
			stmt.close();
			JDBCUtil.closeConnection(conn);
		} 
		catch (SQLException e) {
			System.out.println("Problem with retrieving user information! "	+ e.getMessage());
		}
		return user;
	}
	
	public User selectUserByUid(long uid)
	{
		conn = JDBCUtil.getConnection();
		
		User user = null;
		try {
			stmt = conn.prepareStatement(SELECT_USER_BY_UID);
			stmt.setLong(1, uid);
			
			rs = stmt.executeQuery();
			if (rs.next()) {
				String email = rs.getString("email");
				String password = rs.getString("password");
				String firstname = rs.getString("firstName");
				String lastname = rs.getString("lastName");
				String imageURL = rs.getString("imageURL");
				String summary = rs.getString("summary");
				String city = rs.getString("city");
				String tel = rs.getString("tel");
				int available=rs.getInt("isAvailable");
				user=new User(uid,email,password, firstname,lastname,imageURL, summary,city,tel,available==1);
			}
			stmt.close();
			JDBCUtil.closeConnection(conn);
		} 
		catch (SQLException e) {
			System.out.println("Problem with retrieving user information! "	+ e.getMessage());
		}
		return user;
	}
	public void createUser(User user){
		
	}
}
